﻿package cn.antscript.airCMS.display.component 
{
	import com.bit101.components.HSlider;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	
	/**
	 * ...
	 * @author 
	 */
	public class WaitBar extends Sprite
	{
		private static var _instance:WaitBar;
		
		private var _offset:int = 4;
		
		private var _hSlider:HSlider;
		
		public function WaitBar(privateClass:PrivateClass) 
		{
			_hSlider = new HSlider(this, 0, 0);	
			
			this.mouseChildren = false;
			
		}
		
		public static function getInstance():WaitBar {
			if (_instance == null) {
				_instance = new WaitBar(new PrivateClass);
			}
			return _instance;
		}
		
		public function show(container:DisplayObjectContainer):void {
			
			container.addChild(WaitBar.getInstance());			
			
			container.stage.addEventListener(Event.RESIZE, onResize);
			addEventListener(Event.ENTER_FRAME, onEnter);
			
			onResize();
		}
		public function hide():void {
			
			if (this.parent != null) {
				this.parent.stage.removeEventListener(Event.RESIZE, onResize);
				removeEventListener(Event.ENTER_FRAME, onEnter);
				this.parent.removeChild(this);
			}
			
		}
		
		private function onEnter(e:Event):void 
		{
			_hSlider.value += _offset;
			if (_hSlider.value >= _hSlider.maximum || _hSlider.value<=_hSlider.minimum) {
				_offset *= -1;
			}
			
		}		
		private function onResize(e:Event=null):void 
		{
			graphics.clear();
			graphics.beginFill(0xeeeeee, 0.9);
			graphics.drawRect(0, 0, this.parent.stage.stageWidth, this.parent.stage.stageHeight);
			graphics.endFill();
			
			_hSlider.x = (this.parent.stage.stageWidth - _hSlider.width) / 2;
			_hSlider.y = this.parent.stage.stageHeight / 2;
		}
		
		
	}

}

class PrivateClass {
	
}